package cn.monkey.family.server.domain.sys.address;

import cn.monkey.family.data.sys.address.Address;
import cn.monkey.family.data.sys.address.AddressQueryRequest;
import cn.monkey.family.data.crud.repository.QueryDslQueryRequestRepository;
import cn.monkey.family.data.sys.address.QAddress;
import com.google.common.base.Strings;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface AddressRepository extends QueryDslQueryRequestRepository<Address, String, AddressQueryRequest> {
    @Override
    default Predicate queryDslPredicate(AddressQueryRequest queryRequest) {
        String code = queryRequest.getCode();
        QAddress address = QAddress.address;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        if (!Strings.isNullOrEmpty(code)) {
            booleanBuilder.andAnyOf(address.cityCode.eq(code), address.countryCode.eq(code), address.provinceCode.eq(code), address.streetCode.eq(code));
        }
        String name = queryRequest.getName();
        if (!Strings.isNullOrEmpty(name)) {
            booleanBuilder.andAnyOf(address.cityName.like(name), address.countryName.like(name), address.provinceName.like(name), address.streetName.like(name));
        }
        return booleanBuilder;
    }

}
